/* description

  this series of code is excerpt from:
  http://en.literateprograms.org/Bloom_filter_(C)

  partially modified by He Dongjing, Shanghai Jiao Tong University

  function:
  to check if the input data is in the existed file archive with the method of bloom filter
*/

#include <stdio.h>
#include <string.h>
#include "bloom.h"
#include "check.h"

int main(int argc, char *argv[])
{
	int i;
	FILE *fp;
	char line[1024];
	char *p;
	BLOOM *bloom;
	
	/* here's the structure for the test */
	struct feature_ele input[CHK_SIZE];
	input[0].location = 1000;
	input[1].location = 1001;
	input[2].location = 1002;
	input[3].location = 1003;
	input[4].location = 1004;
	input[5].location = 1005;
	input[6].location = 1006;
	input[7].location = 1007;
	

	if(argc<2) {
		fprintf(stderr, "ERROR: No word file specified\n");
		return EXIT_FAILURE;
	}

	if(!(bloom=bloom_create(2500000, 2, sax_hash, sdbm_hash))) {
		fprintf(stderr, "ERROR: Could not create bloom filter\n");
		return EXIT_FAILURE;
	}

	if(!(fp=fopen(argv[1], "r"))) {
		fprintf(stderr, "ERROR: Could not open file %s\n", argv[1]);
		return EXIT_FAILURE;
	}

	while(fgets(line, 1024, fp)) {
		if((p=strchr(line, '\r'))) *p='\0';
		if((p=strchr(line, '\n'))) *p='\0';

		bloom_add(bloom, line);
	}

	fclose(fp);
	
	for(i=0; i<CHK_SIZE; i++) {
		char buffer[50];
		sprintf(buffer, "%d", input[i].location);
		if(!bloom_check(bloom, buffer)) {
			printf("No match for word %d\n", input[i].location);
		}
		else {
			printf("Match for word %d\n", input[i].location);
		}
	}
	
	bloom_destroy(bloom);

	return EXIT_SUCCESS;
}

